package com.beiing.tianshuai.tianshuai.mine.model;

import com.beiing.tianshuai.tianshuai.entity.FollowBean;
import com.beiing.tianshuai.tianshuai.entity.FollowResultBean;
import com.beiing.tianshuai.tianshuai.entity.PersonalInfoBean;
import com.beiing.tianshuai.tianshuai.network.DataManager;
import com.beiing.tianshuai.tianshuai.util.LogUtils;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.observers.DisposableObserver;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by Jinni on 2017/7/26 0026.
 */

public class MineModel implements MineModelImpl {

    private static final String         TAG = "MineModel";
    private OnRequestListener           mListener;

    public MineModel(OnRequestListener listener) {
        mListener = listener;
    }

    @Override
    public void getPersonalInfoResult(String uid) {
        Observable<PersonalInfoBean> result = DataManager.getPersonalInfoResult(
                uid,
                uid
        );
        Observable<PersonalInfoBean> signUpBeanObservable = result.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
        signUpBeanObservable.subscribe(new DisposableObserver<PersonalInfoBean>() {

            @Override
            public void onNext(@NonNull PersonalInfoBean bean) {
                if (mListener != null) {
                    mListener.onSuccess(bean);
                }
            }

            @Override
            public void onError(@NonNull Throwable e) {
                if (mListener != null) {
                    mListener.onError(e);
                }
            }

            @Override
            public void onComplete() {
                LogUtils.i("------------------ onCompleted ------------------", TAG);
            }
        });
    }

    @Override
    public void isFollowed(String fromUid, String toUid) {
        Observable<FollowBean> result = DataManager.isFollowed(
                fromUid,
                toUid
        );
        Observable<FollowBean> signUpBeanObservable = result.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
        signUpBeanObservable.subscribe(new DisposableObserver<FollowBean>() {

            @Override
            public void onNext(@NonNull FollowBean bean) {
                if (mListener != null) {
                    mListener.isFollowed(bean);
                }
            }

            @Override
            public void onError(@NonNull Throwable e) {
                if (mListener != null) {
                    mListener.onError(e);
                }
            }

            @Override
            public void onComplete() {
                LogUtils.i("------------------ onCompleted ------------------", TAG);
            }
        });
    }

    @Override
    public void setFollowed(String fromUid, String toUid) {
        Observable<FollowResultBean> result = DataManager.getFollowResult(
                fromUid,
                toUid
        );
        Observable<FollowResultBean> signUpBeanObservable = result.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
        signUpBeanObservable.subscribe(new DisposableObserver<FollowResultBean>() {

            @Override
            public void onNext(@NonNull FollowResultBean bean) {
                if (mListener != null) {
                    mListener.followResult(bean);
                }
            }

            @Override
            public void onError(@NonNull Throwable e) {
                if (mListener != null) {
                    mListener.onError(e);
                }
            }

            @Override
            public void onComplete() {
                LogUtils.i("------------------ onCompleted ------------------", TAG);
            }
        });
    }

    public interface OnRequestListener {
        void isFollowed(FollowBean bean);

        void followResult(FollowResultBean bean);

        void onSuccess(PersonalInfoBean bean);

        void onError(Throwable throwable);
    }
}
